<?php

namespace moonlight\slide\actions;

use Yii;
use yii\base\Action;
use moonlight\slide\SlideValidateConfig;
use moonlight\slide\SlideValidateCore;

/**
 * Class VerifyAction 滑动验证验证动作
 */
class VerifyAction extends Action
{
    /**
     * @inheritdoc
     */
    public function run()
    {
        $validate = new SlideValidateCore(SlideValidateConfig::CAPTCHA_ID, SlideValidateConfig::PRIVATE_KEY);
        $session = Yii::$app->getSession();
        $postArray = Yii::$app->getRequest()->post();

        $returnArray = ['status' => 'fail'];
        //服务器正常状态
        if ($session->get('gtserver') == 1) {
            $result = $validate->successValidate($postArray['geetest_challenge'], $postArray['geetest_validate'], $postArray['geetest_seccode'], $session->get('user_id'));
            if ($result) {
                $returnArray = ['status' => 'success'];
            }
        } else {
            //服务器宕机,走failback模式
            if ($validate->failValidate($postArray['geetest_challenge'], $postArray['geetest_validate'], $postArray['geetest_seccode'])) {
                $returnArray = ['status' => 'success'];
            }
        }
        return json_encode($returnArray);
    }
}